﻿using ServiceCore.DbContext;
using ServiceCore.Entity.Core;
using ServiceCore.Entity.Model;

namespace ServiceCore.Service
{
    /// <summary>
    /// 阈值服务
    /// </summary>
    public partial class ThresholdService : SqlsugarServices<ThresholdInfo>
    {
        public BaseParam<ThresholdInfo, long> Threshold_Save_Common_Verify(BaseParam<ThresholdInfo, long> arg)
        {
            if (arg == null)
                return new BaseParam<ThresholdInfo, long>("参数不能为空");
            if (arg.Param == null)
                return new BaseParam<ThresholdInfo, long>("参数不能为空");
            if (string.IsNullOrEmpty(arg.Param.Name))
                return new BaseParam<ThresholdInfo, long>("阈值名称不能为空");           
            if (arg.Param.EndValue == 0)
                return new BaseParam<ThresholdInfo, long>("结束值不能为0");
            //if (arg.Param.CreateUserId == 0)
            //    return new BaseParam<ThresholdInfo, long>("创建人不能为空");

            return arg;
        }

        public BaseParam<ThresholdInfo, long> Threshold_Add_Verify(BaseParam<ThresholdInfo, long> arg)
        {
            arg = Threshold_Save_Common_Verify(arg);
            if (string.IsNullOrEmpty(arg.ErrorMsg) == false)
                return arg;

            var argUser = new BaseParam<ThresholdInfo, ThresholdInfo>();
            argUser.Param = arg.Param;
            Threshold_Exists(argUser, true);
            if (argUser.Data != null)
                return new BaseParam<ThresholdInfo, long>("阈值名称已存在");

            arg.Param.CreateTime = DateTime.Now;
            return arg;
        }

        public BaseParam<ThresholdInfo, long> Threshold_Edit_Verify(BaseParam<ThresholdInfo, long> arg)
        {
            arg = Threshold_Save_Common_Verify(arg);
            if (string.IsNullOrEmpty(arg.ErrorMsg) == false)
                return arg;
            if (arg.Param.Id == 0)
                return new BaseParam<ThresholdInfo, long>("id不能为空");

            var argUser = new BaseParam<ThresholdInfo, ThresholdInfo>();
            argUser.Param = arg.Param;
            Threshold_Exists(argUser, false);
            if (argUser.Data != null)
                return new BaseParam<ThresholdInfo, long>("阈值名称已存在");

            return arg;
        }

        public BaseParam<ThresholdInfo, long> Threshold_Del_Verify(BaseParam<ThresholdInfo, long> arg)
        {
            if (arg == null)
                return new BaseParam<ThresholdInfo, long>("参数不能为空");
            if (arg.Id == 0)
                return new BaseParam<ThresholdInfo, long>("id不能为空");

            return arg;
        }
    }
}
